//
//  RegimenViewController.m
//  味·道
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "RegimenViewController.h"
#import "MaocHeader.h"
#import "RequestManager.h"
#import "ItemModel.h"
#import "RegimenDetailViewController.h"
@interface RegimenViewController ()<RequestManagerDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,retain)UIView *mainView;
@property (nonatomic,retain)UIPickerView *pickerView;
@property (nonatomic,retain)UIButton *skipButton;//跳转Button
@property (nonatomic,retain)UIButton *randomButton;//随机按钮
@property (nonatomic,retain)UILabel *titleLabel;//标题Label
@property (nonatomic,retain)UILabel *detailLabel;//功效label
@property (nonatomic,retain)NSArray *indexesArray;//标题数组
@property (nonatomic,retain)NSMutableArray *dataSourceArray;//数据信息数组
@end

@implementation RegimenViewController
- (void)dealloc
{
    self.mainView = nil;
    self.pickerView = nil;
    self.indexesArray = nil;
    self.dataSourceArray = nil;
    self.titleLabel = nil;
    self.detailLabel = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSourceArray = [NSMutableArray array];
    self.indexesArray = [NSArray array];
    // Do any additional setup after loading the view.
    [self createTitleLabel];
    [self requestData];
    
}
//请求数据
-(void)requestData{
    RequestManager *request = [[RequestManager alloc] initWithRequestURL:@"http://ibaby.ipadown.com/api/food/food.tips.category.php" requestModel:GET requestParameter:nil];
    request.delegate = self;
    [request startRequest];
}
#pragma mark - RequestManagerDelegate
-(void)requestDidFinishWithData:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves) error:nil];
    self.indexesArray = dic[@"indexs"];
    NSArray *array = dic[@"items"];
    for (NSDictionary *dic in array) {
        ItemModel *model = [[ItemModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSourceArray addObject:model];
        [model release];
    }
    [self createMainView];
}
-(void)requestError:(NSError *)error{
    NSLog(@"%@",error);
}
//创建标题
-(void)createTitleLabel{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 30, 80, 20)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    titleLabel.text = @"养生之道";
    [self.view addSubview:titleLabel];
    [titleLabel release];
}
//创建整个view
-(void)createMainView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-55)];
    view.backgroundColor = [UIColor whiteColor];
    self.mainView = view;
    [self.view addSubview:self.mainView];
    [view release];
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 380, 162)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    self.pickerView = pickerView;
    [self.view addSubview:self.pickerView];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 250, 70, 20)];
    label1.text = @"类别:";
    label1.font = [UIFont systemFontOfSize:20.0f];
    [self.mainView addSubview:label1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 250, 100, 20)];
    titleLabel.font = [UIFont systemFontOfSize:20.0f];
    titleLabel.text = @"美容";
    self.titleLabel = titleLabel;
    [self.mainView addSubview:self.titleLabel];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(60, 300, 70, 20)];
    label2.font = [UIFont systemFontOfSize:20.0f];
    label2.text = @"功效:";
    [self.mainView addSubview:label2];
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 300, 100, 20)];
    detailLabel.font = [UIFont systemFontOfSize:20.0f];
    detailLabel.text = @"养颜";
    self.detailLabel = detailLabel;
    [self.mainView addSubview:self.detailLabel];
    UIButton *skipButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    skipButton.frame = CGRectMake(80, 350, 50, 30);
    [skipButton setTitle:@"查看" forState:(UIControlStateNormal)];
    skipButton.titleLabel.font = [UIFont systemFontOfSize:20.0f];
    [skipButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [skipButton addTarget:self action:@selector(skip:) forControlEvents:(UIControlEventTouchUpInside)];
    self.skipButton = skipButton;
    self.randomButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.randomButton.frame = CGRectMake(230, 350, 50, 30);
    [self.randomButton setTitle:@"随机" forState:(UIControlStateNormal)];
    [self.randomButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    self.randomButton.titleLabel.font = [UIFont systemFontOfSize:20.0f];
    [self.randomButton addTarget:self action:@selector(random:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.mainView addSubview:self.randomButton];
    [self.mainView addSubview:self.skipButton];
    [detailLabel release];
    [label1 release];
    [label2 release];
    [titleLabel release];
}
-(void)skip:(UIButton *)sender{
    RegimenDetailViewController *regimenDVC = [[RegimenDetailViewController alloc] init];
    regimenDVC.tag = self.detailLabel.text;
    [self.rightVC.navigationController pushViewController:regimenDVC animated:YES];
}
-(void)random:(UIButton *)sender{
    NSInteger randomC = 0;
   
            NSInteger total = self.indexesArray.count;
            randomC = arc4random()%total;
            NSInteger oldRow = [self.pickerView selectedRowInComponent:0];
            while (randomC == oldRow) {
                randomC = arc4random()%total;
            }
        [self.pickerView selectRow:randomC inComponent:0 animated:YES];
        [self pickerView:nil didSelectRow:randomC inComponent:0];
        [self.pickerView reloadComponent:1];
    NSInteger selectIndex = [self.pickerView selectedRowInComponent:0];
    ItemModel *model = self.dataSourceArray[selectIndex];
    self.detailLabel.text = model.items[0];
 
    
}
#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.indexesArray.count;
    }else{
        NSInteger selectIndex = [self.pickerView selectedRowInComponent:0];
        ItemModel *model = self.dataSourceArray[selectIndex];
        NSArray *itemsArray = model.items;
        return itemsArray.count;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return self.indexesArray[row];
    }else{
        NSInteger selectIndex = [pickerView selectedRowInComponent:0];
        ItemModel *model = self.dataSourceArray[selectIndex];
        return model.items[row];
    }
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        self.titleLabel.text = self.indexesArray[row];
        NSInteger selectIndex = [self.pickerView selectedRowInComponent:0];
        ItemModel *model = self.dataSourceArray[selectIndex];
        self.detailLabel.text = model.items[0];
    }else{
        NSInteger selectIndex = [self.pickerView selectedRowInComponent:0];
        ItemModel *model = self.dataSourceArray[selectIndex];
        self.detailLabel.text = model.items[row];
    }
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
